<?php
/**
 * 退货订管理
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH . 'includes/lib_goods.php');
@ini_set('display_errors',        2);




$step = !empty($_REQUEST['step'])?$_REQUEST['step']:'add';
$smarty->assign('languages', $_LANG);   // 快递类型




/*------------------------------------------------------ */
//-- 退货单列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'back_list')
{   
    /* 检查权限 */
    admin_priv('back_view');
    $smarty->assign('status_list', $_LANG['st']);   // 退货类型
    $smarty->assign('express_list', $_LANG['ex']);   // 快递类型
    $smarty->assign('ur_here',"退货单列表");
    $smarty->assign('action_link', array('href' => 'back.php?act=add', 'text' => '添加退货单'));
    $back_list=back_list();//获取退货单列表





    $smarty->assign('full_page',       1);
    $smarty->assign('filter',       $back_list['filter']);
    $smarty->assign('record_count', $back_list['record_count']);
    $smarty->assign('page_count',   $back_list['page_count']);
    $smarty->assign('back_list',$back_list['back_list']);
    $smarty->display('back_list.htm');
}

//分页
elseif ($_REQUEST['act'] == 'back_query')
{
    $back_list = back_list();//订单列表
    $smarty->assign('filter',       $back_list['filter']);
    $smarty->assign('record_count', $back_list['record_count']);
    $smarty->assign('page_count',   $back_list['page_count']);
    $smarty->assign('back_list',$back_list['back_list']);
    $smarty->assign('languages', $_LANG);   // 快递类型
    make_json_result($smarty->fetch('back_list.htm'), '', array('filter' => $back_list['filter'], 'page_count' => $back_list['page_count']));

}





/*------------------------------------------------------ */
//-- 添加退货单
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
    /* 检查权限 */
    admin_priv('back_view');
    $smarty->assign('ur_here',"添加退货单");
    $smarty->assign('action_link', array('href' => 'back.php?act=back_list', 'text' => '退货单列表'));
    $smarty->assign('status_list', $_LANG['st']);   // 退货类型
    $smarty->assign('express_list', $_LANG['ex']);   // 快递类型
    $order_sn = !empty($_REQUEST['order_sn'])?$_REQUEST['order_sn']:'';//订单号
    //输入订单号生成退货单
    if($step=='add_order_id'){
       $service_type = !empty($_REQUEST['service_type'])?$_REQUEST['service_type']:'';//退货类型
       //查询改订单是否已经有退货单
        $sql = "SELECT return_id FROM ". $ecs->table('order_return_info') .
              " WHERE order_sn = $order_sn ";
        $return_id = $db->getOne($sql);

        if(!empty($return_id)){
        $links[] = array('href' => 'back.php?act=back_list', 'text' => "该退货单已存在,请在退货单列表中搜索");
        sys_msg("该退货单已存在，请在退货单列表中搜索", 100, $links);
        }

        //获取订单的区域ID
        $sql = "SELECT agency_id FROM ".$ecs->table('order_info') ." WHERE order_sn = $order_sn";
        $agency_id = $db->getOne($sql);
        //如果区域ID不存在则订单号错误
        if(empty($agency_id)){
            $links[] = array('href' => 'back.php?act=add', 'text' => "订单号错误");
            sys_msg("订单号错误", 100, $links);
        }
       //插入退货单
        $sql = 'INSERT INTO ' . $ecs->table('order_return_info') . " (order_sn, service_type, add_time, is_push,area_id)" .
                " VALUES ('$order_sn', '$service_type', '".time()."', 1,'$agency_id')";
        $db->query($sql);
        $return_id=$db->insert_id();
        header("Location: back.php?act=add&step=add_goods&return_id=".$return_id."&order_sn=".$order_sn); 
    }
    //添加退货商品
    if($step=='add_goods'){
        //获取订单ID
        $sql = "SELECT order_id FROM ".$ecs->table('order_info') ." WHERE order_sn = $order_sn";
        $order_id = $db->getOne($sql);
        //获取订单商品
        $order_goods=order_goods($order_id);

        $smarty->assign('return_id', $_REQUEST['return_id']);   // 退货单ID
        $smarty->assign('order_goods', $order_goods);   // 退货类型
    
    }

    //完成退货单
    if($step=='complete'){
        $goods_number= !empty($_REQUEST['goods_number'])?$_REQUEST['goods_number']:'';
        $return_id   = !empty($_REQUEST['return_id'])?$_REQUEST['return_id']:'';
        $express_sn  = !empty($_REQUEST['express_sn'])?$_REQUEST['express_sn']:'';
        $express_id  = !empty($_REQUEST['express_id'])?$_REQUEST['express_id']:'';
        //如果没有退货数量则返回错误
    
        if(empty($goods_number)){
            $links[] = array('href' => 'order.php?act=info&order_id='.$order_id, 'text' => "退货数量不能为空");
            sys_msg("退货数量不能为空", 100, $links);
        }
        $goods_number = array_filter($goods_number);
        foreach ($goods_number as $key => $value) {
            $sql =  ' INSERT INTO ' . $ecs->table('order_return_goods') . " (goods_id, goods_name, goods_sku, goods_attr,goods_attr_sku,goods_number,return_id)" .
                    " SELECT goods_id,goods_name,goods_sku,goods_attr,goods_attr_sku,goods_number=$value,@return_id :=$return_id AS return_id ".
                    " FROM ".$ecs->table('order_goods') .
                    " WHERE rec_id = $key";
            $db->query($sql);
        }
        //更新退货单状态
            $sql = " UPDATE ".$ecs->table('order_return_info').
                   " SET is_push = 2,express_sn = $express_sn,express_id = $express_id ".
                   " where return_id = $return_id";
            $db->query($sql);
        $links[] = array('href' => 'back.php?act=back_list', 'text' => "成功添加退货单");
        sys_msg("成功添加退货单", 100, $links);
    }
    $smarty->assign('step',$step);
    $smarty->display('back_add.htm');
}


/*------------------------------------------------------ */
//-- 编辑退货单
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
    /* 检查权限 */
    admin_priv('back_view');
    $smarty->assign('ur_here',"编辑退货单");
    $smarty->assign('action_link', array('href' => 'back.php?act=back_list', 'text' => '退货单列表'));
    $smarty->assign('status_list', $_LANG['st']);   // 退货类型
    $smarty->assign('express_list', $_LANG['ex']);   // 快递类型
    $order_sn   = !empty($_REQUEST['order_sn'])?$_REQUEST['order_sn']:'';
    $return_id   = !empty($_REQUEST['return_id'])?$_REQUEST['return_id']:'';
    $step        = !empty($_REQUEST['step'])?$_REQUEST['step']:'';

    //处理编辑退货单数据
    if($step=='complete'){
        $goods_number= !empty($_REQUEST['goods_number'])?$_REQUEST['goods_number']:'';
        $express_sn  = !empty($_REQUEST['express_sn'])?$_REQUEST['express_sn']:'';
        $express_id  = !empty($_REQUEST['express_id'])?$_REQUEST['express_id']:'';
        $service_type  = !empty($_REQUEST['service_type'])?$_REQUEST['service_type']:'';

        //更新退货数量
        foreach ($goods_number as $key => $value) {
            $sql = " UPDATE ".$ecs->table('order_return_goods').
                   " SET goods_number = $value ".
                   " where rec_id = $key";
            $db->query($sql);
        }
        //更新退货单信息
        $sql = " UPDATE ".$ecs->table('order_return_info').
                   " SET is_push = 2,express_sn = $express_sn,express_id = $express_id ,service_type = $service_type".
                   " where return_id = $return_id";
        $db->query($sql);
        $links[] = array('href' => 'back.php?act=back_list', 'text' => "修改退货单成功");
        sys_msg("修改退货单成功", 100, $links);
    }
    $return_info = back_info($return_id);
    $smarty->assign('return_info',$return_info);
    $smarty->assign('return_id',$return_id);

    $smarty->display('back_edit.htm');
}
/*------------------------------------------------------ */
//-- 获取商品
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'get_goods'){
        include_once(ROOT_PATH . 'includes/cls_json.php');
        $json = new JSON();
         /* 通过商品ID获取商品信息 */
        $goods_id = $_REQUEST['goods_id'];//商品ID
        //查询属性表判断是否有属性
                $sql="SELECT attr_sku FROM ". $ecs->table('goods_attr') ." WHERE goods_id = $goods_id";
                $goods_attr=$db->getOne($sql);
                    if($goods_attr){
                        $sql="SELECT g.goods_id,g.goods_sku,g.goods_name,ga.attr_value,ga.attr_sku ".
                        " FROM ".$ecs->table('goods') ." AS g".
                        " LEFT JOIN ".$ecs->table('goods_attr') ." AS ga ON g.goods_id = ga.goods_id ".
                        " WHERE g.goods_id = $goods_id ";
                        $goods=$db->getAll($sql);
                        //重组数据
                        foreach ($goods as $key => $value) {
                            $goods_list['goods_name']=$value['goods_name'];
                            $goods_list['goods_sku']=$value['goods_sku'];
                            $goods_list['attr_list'][$key]['attr_value']=$value['attr_value'];
                            $goods_list['attr_list'][$key]['attr_sku']=$value['attr_sku'];
                        }
                        
                    }
                    else{
                       $sql="SELECT g.goods_id,g.goods_sku,g.goods_name".
                        " FROM ".$ecs->table('goods') ." AS g".
                        " WHERE g.goods_id = $goods_id ";
                        $goods_list=$db->getRow($sql);

                    }

         $smarty->assign('goods', $goods_list);
         $return = $smarty->fetch("back/order_goods.htm");
         echo $json->encode($return);



}
/*------------------------------------------------------ */
//-- 商品加入到退货单
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'get_goods_complete')
{ 

        include_once(ROOT_PATH . 'includes/cls_json.php');
        $json = new JSON();
        $goods_info  = $json->decode($_REQUEST['goods_info']);
        $attr        = $json->decode($_REQUEST['attr']);
        $return_id   = !empty($_REQUEST['return_id'])?$_REQUEST['return_id']:'';
        // 有属性处理
        if($attr){
            $goods_id = $goods_info->goods_id;
            foreach ($attr as $key => $value) {
                    //查询属性信息
                    $sql="SELECT attr_value,goods_attr_id,attr_sku ".
                         'FROM ' . $GLOBALS['ecs']->table('goods_attr') .
                         " WHERE attr_sku =$key";
                    $attr_info=$db->getRow($sql);
                    $sql =  ' INSERT INTO ' . $ecs->table('order_return_goods') . " (goods_id, goods_name, goods_sku,goods_number,return_id,goods_attr,goods_attr_sku)" .
                    " SELECT goods_id,goods_name,goods_sku,$value,@return_id :=$return_id AS return_id,'$attr_info[attr_value]',$attr_info[attr_sku] ".
                    " FROM ".$ecs->table('goods') .
                    " WHERE goods_id = $goods_id";
                    $db->query($sql);
            }
        }
        else{
            /* 取得参数 */
            $goods_id = $goods_info->goods_id;
            $goods_number=$goods_info->goods_number;//商品数量
            $sql =  ' INSERT INTO ' . $ecs->table('order_return_goods') . " (goods_id, goods_name, goods_sku,goods_number,return_id)" .
                    " SELECT goods_id,goods_name,goods_sku,$goods_number,@return_id :=$return_id AS return_id ".
                    " FROM ".$ecs->table('goods') .
                    " WHERE goods_id = $goods_id";
            $db->query($sql);
        }

        


        $return_info = back_info($return_id);
        $smarty->assign('return_info',$return_info);
        $smarty->assign('return_id',$return_id);
        $return['error']   = 1; 
        $return['content'] = $smarty->fetch("back/order_goods_list.htm");
        echo $json->encode($return);
        exit();



}

/*------------------------------------------------------ */
//-- 删除退货单
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'delete')
{   
    $return_id =!empty($_REQUEST['return_id'])?$_REQUEST['return_id']:'';
    $sql = " DELETE FROM ". $ecs->table('order_return_goods').
           " WHERE return_id = ".$return_id;
    $db->query($sql);
    $sql = " DELETE FROM ". $ecs->table('order_return_info').
           " WHERE return_id = ".$return_id;
    $db->query($sql);
    require(ROOT_PATH . 'includes/lib_wangcang.php');
    vsetcancelBackTrade(array($return_id), $v_data);
    header("Location: back.php?act=back_list");
}
/*------------------------------------------------------ */
//-- 删除退货商品
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'delete_goods')
{   
    $rec_id =!empty($_REQUEST['rec_id'])?$_REQUEST['rec_id']:'';
    $sql = " DELETE FROM ". $ecs->table('order_return_goods').
           " WHERE rec_id = ".$rec_id;
    $db->query($sql);
}



/*------------------------------------------------------ */
//-- 确认处理退货单
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'handle')
{   
    $return_id =!empty($_REQUEST['return_id'])?$_REQUEST['return_id']:'';
    //更新退货单状态
    $sql = " UPDATE ".$ecs->table('order_return_info').
           " SET is_push = 5 ".
           " where return_id = $return_id";
    $db->query($sql);
    header("Location: back.php?act=back_list"); 
}








/*------------------------------------------------------ */
//-- 查看退货单
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'info')
{   
    $smarty->assign('ur_here',"退货单详情");
    $smarty->assign('action_link', array('href' => 'back.php?act=back_list', 'text' => '退货单列表'));
    $return_id =!empty($_REQUEST['return_id'])?$_REQUEST['return_id']:'';
    $return_info = back_info($return_id);
    $smarty->assign('return_info',$return_info);
    $smarty->display('back_info.htm');
}




/*退货单详情*/
function back_info($return_id){
    $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('order_return_info')." WHERE return_id = $return_id ";
    $return_info = $GLOBALS['db']->getRow($sql);
    $return_info['add_time'] =date('Y-m-d-H:m:s',$return_info['add_time']);
    $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('order_return_goods')." WHERE return_id = $return_id ";
    $return_goods = $GLOBALS['db']->getAll($sql);
    $return_info['goods_list']=$return_goods;


    return $return_info;
}






/*退货单列表*/
function back_list(){



        $where = 'WHERE 1 ';
       

        /* 分页大小 */
        $filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']);
        if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0)
        {
            $filter['page_size'] = intval($_REQUEST['page_size']);
        }
        elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0)
        {
            $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']);
        }
        else
        {
            $filter['page_size'] = 1;
        }

        $sql= "SELECT * FROM ".$GLOBALS['ecs']->table('order_return_info') .$where."  ORDER BY add_time DESC";



        $filter['record_count'] = $GLOBALS['db']->num_rows($GLOBALS['db']->query($sql)); // 总数
        $filter['page_count']     = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1;//总数
        $sql = $sql." LIMIT " . ($filter['page'] - 1) * $filter['page_size'] . ",$filter[page_size]";
        $row = $GLOBALS['db']->getAll($sql);

        foreach ($row as $key => $value) {
            $row[$key]['add_time'] =date('Y-m-d-H:m:s',$value['add_time']);
        }







        $arr = array('back_list' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
        return $arr;
}



?>